using System;
using System.Collections.Generic;
using System.Linq;
using Giraffe.Core.Common.Messengers;

namespace Giraffe.Core.Model
{
    public class Contact : IContact
    {
        private readonly string name;
        private List<IMessengerContact> _messengerContacts;

        public event Action StateChanged;
        
        public Contact(IEnumerable<IMessengerContact> contacts)
        {
            //todo change logic, load name from settings
            name = contacts.First().Name;
            _messengerContacts = new List<IMessengerContact>();
            _messengerContacts.AddRange(contacts);

            foreach (IMessengerContact contact in contacts)
            {
                contact.StateChanged += OnMessaengerContactStaeChanged; 
            }
        }

        public IEnumerable<IMessengerContact> MessengerContacts
        {
            get { return _messengerContacts; }
        }

        public IMessengerContact GetMessaengerContact(MessengerType messenger)
        {
            return MessengerContacts.First(mContact => mContact.Messenger == messenger);
        }

        private void OnMessaengerContactStaeChanged()
        {
            if (StateChanged != null)
            {
                StateChanged();
            }
        }

        public string Name
        {
            get { return name; }
        }

        //todo change logic
        public ContactStateEnum State
        {
            get
            {
                return MessengerContacts.First().State;
            }
        }
    }
}